<?php

namespace common\models;

use Yii;
use services\models\Goods;
use \yii\data\Pagination;

use yii\widgets\LinkPager;

use services\models\CommentsRelation;

use services\models\ProductSpec;
use services\models\Product;
use services\models\GoodsExt;
use services\models\SpecQuery;
use services\models\SpecValue;
use services\models\SpecTypeQuery;
use services\models\ProductStock;

/**
 * 通过redis获取商品
 * @author zhouqi
 * 
 */ 
class GoodsRedis
{
    /**
     * 获取单个商品
     */
    public static function getRedisList($goods_id)
    {
        $list_key = ['goods_id','goods_name','goods_desc','goods_main','sku','defaultsku','status','auto_time','goods_auto_dw','need_show'];
        $list_arr = Yii::$app->redis_goods->hmget('G_'.$goods_id,$list_key);
        $list_arr = !empty($list_arr)?$list_arr:'';
        return $list_arr;
    }
    
    public static function getRedisGoodsList($goods_id)
    {
        $list_key = ['goods_id','goods_name','goods_desc','status','goods_auto_upv','goods_auto_dw','need_show'];
        $list_arr = Yii::$app->redis_goods->hmget('goods_'.$goods_id,$list_key);
        $list_arr = !empty($list_arr)?$list_arr:'';
        return $list_arr;
    }
    
    public static function getRedisMain($goods_id)
    {
        $list_arr = Yii::$app->redis_goods->get('goods_main_'.$goods_id);
        $list_arr = !empty($list_arr)?$list_arr:'';
        return $list_arr;
    }
    
    /**
     * 获取一个货品的信息（SKN）
     */
    public static function getRedisProduct($product_id)
    {
        $list_key = ['goods_id','sku1','sku2','type','sale_price','try_price'];
        $list_arr = Yii::$app->redis_goods->hmget('P_'.$product_id,$list_key);
        $list_arr = !empty($list_arr)?$list_arr:'';
        return $list_arr;
    }
    
    /**
     * 获取SKU数据
     */
    public static function getRedisProductMore($product_id)
    {
        $list_key = ['spec'];
        $list_arr = Yii::$app->redis_goods->hmget('P_S_'.$product_id,$list_key);
        $list_arr = !empty($list_arr)?$list_arr:'';
        return $list_arr;
    }
    
    /**
     * 通过$spec_id,$spec_value_id获取SKU数据
     * @param SKUID $spec_id
     * @param SKU值ID $spec_value_id
     * @return array
     */
    public static  function getRedisProductBySpec($spec_id,$spec_value_id,$goods_id) 
    {
        $list_arr = Yii::$app->redis_goods->hgetall('S_P_'.$spec_id.'_'.$spec_value_id.'_'.$goods_id);
        $list_arr = !empty($list_arr)?$list_arr:'';
        return $list_arr;
    }
    
    /**
     * 获取品牌导航缓存
     * @return array
     */
    public static  function getRedisQindex()
    {
        $list_key = ['q_index'];
        $list_arr = Yii::$app->redis_goods->hmget('G_Brands_Qindex',$list_key);
        $list_arr = !empty($list_arr)?$list_arr:'';
        return $list_arr;
    }
    
    /**
     * 获取品牌缓存
     * @return array
     */
    public static  function getRedisBrands()
    {
        $list_key = ['brandname','id','logo','q_index'];
        $list_arr = Yii::$app->redis_goods->hmget('G_Brands',$list_key);
        $list_arr = !empty($list_arr)?$list_arr:'';
        return $list_arr;
    }
    
    /**
     * 获取当前商品所有规格，价格和类型 redis
     * @param 商品ID $product_id
     * @return array
     */
    public static function getSkuRedis($product_id) {
        $list_info = self::getRedisProduct($product_id);
        if(empty($list_info['goods_id'])) {
            //判断商品是否有效
            $p_list = Product::findByProductId($product_id);
            if(empty($p_list['product_id'])) {
                $list_info['goods_id'] = NULL;
                Yii::$app->redis_goods->hmset('P_'.$product_id,$list_info);
                return $list_info;
            }
            $list = ProductSpec::findByProductIdByDefault($product_id);
            $list_info['goods_id'] = !empty($list[0]['goods_id'])?$list[0]['goods_id']:'';
            $list_info['sku1'] = !empty($list[0]['spec_value_id'])?$list[0]['spec_value_id']:'';
            $list_info['sku2'] = !empty($list[1]['spec_value_id'])?$list[1]['spec_value_id']:'';
            if (empty($list_info['goods_id'])) {
                $list = [];
                $list = Product::findByProductId($product_id);
                if (empty($list['goods_id'])) {
                    $list_info['goods_id'] = NULL;
                    Yii::$app->redis_goods->hmset('P_'.$product_id,$list_info);
                    return $list_info;
                }
                $list_info['goods_id'] = !empty($list['goods_id'])?$list['goods_id']:'';
                $list_info['sku1'] = '';
                $list_info['sku2'] = '';
            }
            if (!empty($list_info['sku2'])) {
                $notme = self::getSpecAllByGoodsId($list_info['goods_id'],$list_info['sku1'],$list_info['sku2']);
                foreach ($notme as $nmkey=>$nmval) {
                    if ($nmval['product_id'] != $product_id) {
                        $notme[0]['product_id'] = $nmval['product_id'];
                    }
                }
            }
            if(empty($list_info['sku2'])) {
                $notme = ProductSpec::findProductByGoodsIdAndSpecIdNotMe($list_info['goods_id'],$list_info['sku1'],$product_id);
            }
            if(empty($list_info['sku2']) && empty($list_info['sku1'])) {
                $product_list = Product::findByGoodsIdAll($list_info['goods_id']);
                $flag = 0;
                foreach ($product_list as $pkey=>$pval) {
                    if ($pval['type'] == 1) {
                        if ($pval['price_type'] == 0) {
                            $list_info['try_price'] = '¥'.sprintf(" %1\$.2f",$pval['sale_price']);
                        } elseif ($pval['price_type'] == 1) {
                            $list_info['try_price'] = $pval['try_price'].'试豆';
                        }
                        $flag = $flag + 1;
                    } elseif($pval['type'] == 2) {
                        if ($pval['price_type'] == 0) {
                            $list_info['sale_price'] = '¥'.sprintf(" %1\$.2f",$pval['sale_price']);
                        } elseif ($pval['price_type'] == 1) {
                            $list_info['sale_price'] = $pval['try_price'].'试豆';
                        }
                        $flag = $flag + 1;
                    }
                    $list_info['type'] = $pval['type'];
                }
                if($flag == 2){
                    $list_info['type'] = 3;
                }
            } else {
                //获取当前商品价格
                $product_list = Product::findByProductId($product_id);
                if (!empty($product_list['type']) && $product_list['type'] == 1) {
                    if ($product_list['price_type'] == 0) {
                        $list_info['try_price'] = '¥'.sprintf(" %1\$.2f",$product_list['sale_price']);
                    } elseif ($product_list['price_type'] == 1) {
                        $list_info['try_price'] = $product_list['try_price'].'试豆';
                    }
                } elseif(!empty($product_list['type']) && $product_list['type'] == 2) {
                    if ($product_list['price_type'] == 0) {
                        $list_info['sale_price'] = '¥'.sprintf(" %1\$.2f",$product_list['sale_price']);
                    } elseif ($product_list['price_type'] == 1) {
                        $list_info['sale_price'] = $product_list['try_price'].'试豆';
                    }
                }
                $list_info['type'] = $product_list['type'];
                if(!empty($notme[0]['product_id'])) {
                    $product_list = Product::findByProductId($notme[0]['product_id']);
                    if(!empty($product_list['type']) && $product_list['type'] == 1) {
                        if ($product_list['price_type'] == 0) {
                            $list_info['try_price'] = '¥'.sprintf(" %1\$.2f",$product_list['sale_price']);
                        } elseif ($product_list['price_type'] == 1) {
                            $list_info['try_price'] = $product_list['try_price'].'试豆';
                        }
                    }
                    elseif(!empty($product_list['type']) && $product_list['type'] == 2) {
                        if ($product_list['price_type'] == 0) {
                            $list_info['sale_price'] = '¥'.sprintf(" %1\$.2f",$product_list['sale_price']);
                        } elseif ($product_list['price_type'] == 1) {
                            $list_info['sale_price'] = $product_list['try_price'].'试豆';
                        }
                    }
                }
            }
            if(!empty($list_info['sale_price']) && !empty($list_info['try_price'])) {
                $list_info['type'] = 3;
            }
            Yii::$app->redis_goods->hmset('P_'.$product_id,$list_info);
        }
        return $list_info;
    }
    
    /**
     * 产品详情缓存
     * @param 产品 $goods_id
     * @return array
     */
    public static function getListRedis($goods_id) {
        $list_info=[];
        $list_info = self::getRedisList($goods_id); 
        if(empty($list_info['goods_id'])) {
            //获取商品信息
            $goods_details = self::getRedisGoodsList($goods_id);
            if (empty($goods_details['goods_id'])) {
                return '';
            }
            $list_info['goods_id'] = $goods_details['goods_id'];
            $list_info['goods_name'] = $goods_details['goods_name'];
            $list_info['status'] = $goods_details['status'];
            $list_info['auto_time'] = $goods_details['goods_auto_upv']; //上架时间
            $list_info['goods_auto_dw'] = $goods_details['goods_auto_dw']; //下架时间
            $list_info['need_show'] = !empty($goods_details['need_show'])?$goods_details['need_show']:0; //2000W-4000W是否展示 （0，不展示 1，展示）
            //获取商品详细信息
            $goods_ext = GoodsExt::findByGoodsId($goods_id);
            $list_info['goods_desc'] = !empty($goods_ext['goods_desc'])?$goods_ext['goods_desc']:'';
            $list_info['goods_main'] = self::getRedisMain($goods_id);
            $list = ProductSpec::findSkuByGoodsId($goods_id);
            if(!empty($list[0]['spec_id'])) {
                //TO spec_id
                foreach ($list as $key=>$val) {
                    $sku = SpecQuery::getListById($val['spec_id']);
                    $sku_arr[$sku['id']]['spec_id'] = $sku['id'];
                    $sku_arr[$sku['id']]['name'] = $sku['name'];
                    $sku_arr[$sku['id']]['type_id'] = $list[0]['type_id'];
                    $order_son_arr[$key]['id'] = $sku['id'];
                    $order_son_arr[$key]['type_id'] = $list[0]['type_id'];
                }
                //TO spec_value_id
                $list_val = ProductSpec::findSkuByGoodsIdToValue($goods_id);
                foreach ($list_val as $skukey=>$skuval) {
                    $sku1[] = SpecValue::getListById($skuval['spec_value_id']);
                }
                //组合
                foreach ($sku_arr as $allkey=>$allval) {
                    foreach ($sku1 as $k2=>$v2) {
                        if($allval['spec_id'] == $v2['spec_id']) {
                            $sku_arr[$allval['spec_id']]['childs'][$v2['id']]['spec_value_id'] = $v2['id'];
                            $sku_arr[$allval['spec_id']]['childs'][$v2['id']]['name'] = $v2['name'];
                        }
                    }
                }
                //排序
                $order_list = SpecTypeQuery::getSpecOrder($order_son_arr);
                $list_info['sku'] = [];
                foreach ($order_list as $allkey=>$allval) {
                    foreach ($sku_arr as $k2=>$v2) {
                        if($allval['spec_id'] == $k2) {
                            $list_info['sku'][] = $sku_arr[$k2];
                        }
                    }
                }
                if(!empty($list_info['sku'])){
                    $list_info['sku'] = serialize($list_info['sku']);
                } else {
                    $list_info['sku'] = '';
                }
            }
            //获取当前商品下的默认SKU
            $defaultsku = Product::findByGoodsIdDefault($goods_id);
            $list_info['defaultsku'] = !empty($defaultsku['product_id'])?$defaultsku['product_id']:'';
            Yii::$app->redis_goods->hmset('G_'.$goods_id,$list_info);
        }
        if(!empty($list_info['sku'])){
            $list_info['sku'] = unserialize($list_info['sku']);
            //当前选中规格下所有商品是否有库存
            $spec_id = $list_info['sku'][0]['spec_id'];
            $shift_arr = $list_info['sku'][0]['childs'];
            foreach ($shift_arr as $sk=>$sv) {
                $togray = self::toGray($spec_id,$sv['spec_value_id'],$goods_id);
                if(is_array($togray)) {
                    foreach ($togray as $gk=>$gv) {
                        if(!isset($gv['idgray'])) {
                            unset($togray[$gk]);
                        }
                    }
                    if(empty($togray)){
                        $list_info['sku'][0]['childs'][$sk]['idgray'] = 0;
                    } else {
                        foreach ($togray as $k2=>$v2) {
                            if($sv['spec_value_id'] == $v2['spec_value_id']){
                                $list_info['sku'][0]['childs'][$sk]['idgray'] = $v2['idgray'];
                            }
                        }
                    }
                }
            }
            $list_info['sku'] = serialize($list_info['sku']);
        } else {
            $list_info['sku'] = '';
        }
        return $list_info;
    }
    
    /**
     * 根据 $spec_id $spec_value_id $goods_id 确定规格按钮是否置灰
     * @param 规格ID $spec_id
     * @param 规格值 $spec_value_id
     * @param 产品ID $goods_id
     * @return Ambigous <multitype:, number>
     */
    public static function toGray($spec_id,$spec_value_id,$goods_id) {
        $product_spec = [];
        $product_spec = self::getRedisProductBySpec($spec_id,$spec_value_id,$goods_id);
        if(empty($product_spec)) {
            $product_spec = [];
            //根据当前规格获取PRODUCT_ID
            $product_spec_list = ProductSpec::findSkuBySpecIdAndSpecValueIdAndGoodsId($spec_id,$spec_value_id,$goods_id);

            foreach ($product_spec_list as $k=>$v) {
                //判断商品是否可用
                $isinfo = [];
                $isinfo = Product::findByProductId($v['product_id']);
                if (!empty($isinfo['product_id'])) {
                    $product_spec[] = $v['product_id'];
                }
            }
            Yii::$app->redis_goods->del('S_P_'.$spec_id.'_'.$spec_value_id);
            Yii::$app->redis_goods->hmset('S_P_'.$spec_id.'_'.$spec_value_id.'_'.$goods_id,$product_spec);
        }
        //获取这些商品对应的所有SKU组合
        $flag = 0;
        $p_arr = [];
        foreach ($product_spec as $k1=>$v1) {
            $p_arr = [];
            $p_arr = self::getRedisProductMore($v1);
            if (empty($p_arr['spec'])) {
                $p_list = ProductSpec::findSkuBySpecIdAndSpecValueIdAndGoodsId($spec_id,$spec_value_id,$goods_id);
                foreach ($p_list as $k2=>$v2) {
                    if($v1 != $v2['product_id']) {
                        continue;
                    }
                    $p_arr[$k2]['product_id'] = $v2['product_id'];
                    $p_arr[$k2]['spec_id'] = $v2['spec_id'];
                    $p_arr[$k2]['spec_value_id'] = $v2['spec_value_id'];
                }
                //序列化
                foreach ($p_arr as $k3=>$v3) {
                    $p_arr['spec'][$k3] = $p_arr[$k3];
                    unset($p_arr[$k3]);
                }
                if(!empty($p_arr['spec'])) {
                    $p_arr['spec'] = serialize($p_arr['spec']);
                }
                Yii::$app->redis_goods->del('P_S_'.$v1);
                Yii::$app->redis_goods->hmset('P_S_'.$v1,$p_arr);
            }
            if(!empty($p_arr['spec'])){
                $p_arr = unserialize($p_arr['spec']);
            } else {
                $p_arr = [];
            }
            foreach ($p_arr as $k3=>$v3) {
                $stock = [];
                //查询商品库存
                $stock = ProductStock::findByProductId($v3['product_id']);
                if (isset($stock['stock']) && $stock['stock'] > 0) {
                    if(!empty($p_arr[$k3]['idgray']) && $p_arr[$k3]['idgray'] == 1){
                        continue;
                    }
                    $p_arr[$k3]['idgray'] = 1;
                    $flag = 1;
                } else {
                    $p_arr[$k3]['idgray'] = 0;
                }
                if($flag == 1) {
                    break;
                }
            }
            if($flag == 1) {
                break;
            }
        }
        return $p_arr;
    }
    
    /**
     * 通过GOODSID判断是否可以跳转规格
     * @param $request
     * @return multitype:number string multitype: NULL unknown
     */
    public static function toJumping($request) {
        $goods_id = !empty($request['goods_id'])?$request['goods_id']:'';
        $sku1 = !empty($request['sku1'])?$request['sku1']:'';
        $sku2 = !empty($request['sku2'])?$request['sku2']:'';
        //获取商品ID
        if(!empty($sku2)) {
            $product_list = self::getSpecAllByGoodsId($goods_id,$sku1,$sku2);
        } else {
            $product_list = ProductSpec::findProductByGoodsIdAndSpecId($goods_id,$sku1);
            //判断product_id是否有效
            foreach ($product_list as $k=>$v) {
                $info = Product::findByProductId($v['product_id']);
                if (empty($info['product_id'])) {
                    unset($product_list[$k]);
                }
            }
            shuffle($product_list);
        }
        $flag = 0;
        if(empty($product_list[0]['product_id'])) {
            $data['flag'] = 1;
            $products['goods_id'] = $goods_id;
            $products['sku1'] = $sku1;
            $products['sku2'] = $sku2;
            $goods_list = self::getListRedis($goods_id);
            $data['sku'] = unserialize($goods_list['sku']);
            $data['products'] = $products;
        } else {
            foreach ($product_list as $pkey=>$pval) {
                $mystock = ProductStock::findByProductId($pval['product_id']);
                if ($mystock['stock'] > 0) {
                    $flag = 1;
                    break;
                }
            }
            //判断是否有库存
            $data = array('flag'=>0,'data'=>'','desc'=>array());
            if($flag == 0) {
                $data['flag'] = 1;
                $products = self::getSkuRedis($product_list[0]['product_id']);
                $goods_list = self::getListRedis($goods_id);
                $data['sku'] = unserialize($goods_list['sku']);
                $data['products'] = $products;
            } else {
                $data['data'] = $product_list[0]['product_id'];
            }
        }
        return $data;
    }
    
    /**
     * 获取商品数据
     */
    public static function getGoodsListByRedis ($request) {
        $product_id = !empty($request['product_id'])?$request['product_id']:0;
        //根据productID找GOODSID
        $products = self::getSkuRedis($product_id);
        if(empty($products['goods_id'])) {
            $return_arr['error'] = '商品不存在';
            return $return_arr;
        }
        //获取商品ID
        $goods_id = !empty($products['goods_id'])? $products['goods_id'] : 0;
        //返回数组
        $goods_info = [];
        //获取产品信息
        $goods_list = [];
        $goods_list = self::getListRedis($goods_id);
        if (empty($goods_list)) {
            $return_arr['error'] = '商品不存在';
            return $return_arr;
        }
        $goods_info['goods_id'] = $goods_id;
        $goods_info['goods_name'] = $goods_list['goods_name']; //商品名称
        $goods_info['image'] = Goods::findByGoodsIdFromRedis($goods_id, ['pic', 'bigpic', 'compress_thumb', 'gallery_list']); //商品图片
        $goods_info['gallery_list'] = unserialize($goods_info['image']['gallery_list']);
        $goods_info['try_price'] = !empty($products['try_price'])?$products['try_price']:0;
        $goods_info['sale_price'] = !empty($products['sale_price'])?$products['sale_price']:0;
        $goods_info['type'] = $products['type'];
        $goods_info['defaultsku'] = $goods_list['defaultsku'];
        $goods_info['need_show'] = $goods_list['need_show'];
        if(!empty($goods_list['sku'])) {
            $goods_info['sku'] = unserialize($goods_list['sku']);
        }
        //商品详细信息
        $goods_info['goods_desc'] = $goods_list['goods_desc']; //商品副标题
        $goods_info['goods_main'] = $goods_list['goods_main']; //商品详情
        $goods_info['status'] = $goods_list['status'];
        $goods_info['auto_time'] = $goods_list['auto_time']; //上架时间
        $goods_info['goods_auto_dw'] = $goods_list['goods_auto_dw']; //下架时间
        $sku1 = '';
        $sku2 = '';
        //根据商品ID获取对应 （试用，购买）
        if(empty($products['sku2']) && empty($products['sku1'])) {
            $product_list = Product::findByGoodsIdAll($goods_id);
            foreach ($product_list as $k=>$v) {
                $plist = [];
                $plist = Product::findByProductId($v['product_id']);
                $keys = 'stype';
                if(!empty($plist['type']) && $plist['type'] == 2) {
                    $keys = 'stype1';
                }
                if (!empty($plist['type']) && $plist['type'] == 1) {
                    $goods_info['try_pid'] = $v['product_id'];
                }
                if (!empty($plist['type']) && $plist['type'] == 2) {
                    $goods_info['sale_pid'] = $v['product_id'];
                }
                $pslist = ProductStock::findByProductId($v['product_id']);
                if(!empty($pslist['stock']) && $pslist['stock'] > 0) {
                    $goods_info[$keys] = 1;
                } else {
                    $goods_info[$keys] = 0;
                }
            }
        } else {
            if(!empty($products['sku2'])) {
                $notme = self::getSpecAllByGoodsId($goods_id,$products['sku1'],$products['sku2']);
                foreach ($notme as $nmkey=>$nmval) {
                    if ($nmval['product_id'] != $product_id) {
                        $notme[0]['product_id'] = $nmval['product_id'];
                    }
                }
                $product_list[]['product_id'] = $product_id;
                if (!empty($notme[0]['product_id'])) {
                    $product_list[]['product_id'] = $notme[0]['product_id'];
                }
            } else {
                $product_list = ProductSpec::findProductByGoodsIdAndSpecId($goods_id,$products['sku1']);
            }
            foreach ($product_list as $k=>$v) {
                $plist = [];
                $plist = Product::findByProductId($v['product_id']);
                $keys = 'stype';
                if(!empty($plist['type']) && $plist['type'] == 2) {
                    $keys = 'stype1';
                }
                if (!empty($plist['type']) && $plist['type'] == 1) {
                    $goods_info['try_pid'] = $v['product_id'];
                }
                if (!empty($plist['type']) && $plist['type'] == 2) {
                    $goods_info['sale_pid'] = $v['product_id'];
                }
                $pslist = ProductStock::findByProductId($v['product_id']);
                if(!empty($pslist['stock']) && $pslist['stock'] > 0) {
                    $goods_info[$keys] = 1;
                } else {
                    $goods_info[$keys] = 0;
                }
            }
        }
        $CommentsModel = new CommentsRelation();
        $goods_info['reply_back_num'] = $CommentsModel->getFeedbackTotal($goods_id);
        $return_arr['goods_info'] = $goods_info;
        $return_arr['products'] = $products;
        return $return_arr;
    }
    
    /**
     * 通过redis获取评论
     * @param $request
     * @return string
     */
    public static function getReplyByRedis($request) {
        $product_id = !empty($request['product_id'])?$request['product_id']:0;
        $backRatetype = !empty($request['backRatetype'])?$request['backRatetype']:'';
        //根据productID找GOODSID
        $products = self::getSkuRedis($product_id);
        $goods_id = $products['goods_id'];
        //获取当前页数
        $page = !empty($request['page'])?$request['page']:1;
        $count = 10;
        
        //好中差评率
        $CommentsModel = new CommentsRelation();
        $backRate = $CommentsModel->getFeedbackRate($goods_id);
        $backNum = $CommentsModel->getFeedbackNumber($goods_id);
        //商品评论信息
        if ($backRatetype == '') {
            $reply_ids = $CommentsModel->getGoodsCommentsIds($goods_id);
        } else {
            $reply_ids = $CommentsModel->getGoodsCommentsIds($goods_id,$backRatetype);
        }
        $arr_count = $CommentsModel->getFeedbackTotal($goods_id);
        
        //当前页
        if(is_array($reply_ids)) {
            if($page == 1) {
                $reply_ids = array_slice($reply_ids,0,$count);
            } else {
                $firstcount = $count*($page-1);
                $reply_ids = array_slice($reply_ids,$firstcount,$count);
            }
        } else {
            $reply_ids = [];
        }
        $reply_info = $CommentsModel->getGoodsCommentsByIds($reply_ids);
        
        $thispage = new Pagination(['totalCount' => $arr_count, 'pageSize' => $count]);
        $thispage = LinkPager::widget(['pagination' => $thispage]);
        
        
        $return_arr['page'] = $thispage;
        $return_arr['backRate'] = $backRate;
        $return_arr['backNum'] = $backNum;
        $return_arr['reply_info'] = $reply_info;
        $return_arr['backRate'] = $backRate;
        return $return_arr;
    }
    
    /**
     * 根据GOODS_ID 和两个规格值ID获取product_id
     * @param 商品ID $goods_id
     * @param 规格1 $sku1
     * @param 规格2 $sku2
     * @return Ambigous
     */
    public static function getSpecAllByGoodsId($goods_id,$sku1,$sku2) {
        $return_arr = Yii::$app->redis_goods->get('SPEC_ALL_'.$goods_id);
        if(empty($return_arr)) {
            $alllist = ProductSpec::findByGoodsId($goods_id);
            foreach ($alllist as $k=>$v) {
                //判断商品是否存在
                $isinfo = Product::findByProductId($v['product_id']);
                if(empty($isinfo['product_id'])) {
                    continue;
                }
                $return_arr[$v['product_id']]['product_id'] = $v['product_id'];
                if(empty($return_arr[$v['product_id']]['spec_value_id'])){
                    $return_arr[$v['product_id']]['spec_value_id'] = $v['spec_value_id'];
                } else {
                    if ($return_arr[$v['product_id']]['spec_value_id'] != $v['spec_value_id']) {
                        $return_arr[$v['product_id']]['spec_value_id1'] = $v['spec_value_id'];
                    }
                }
            }
            $return_arr = serialize($return_arr);
            Yii::$app->redis_goods->set('SPEC_ALL_'.$goods_id,$return_arr);
        }
        $return_arr = unserialize($return_arr);
        $arr = [];
        //获取当前值的PID
        foreach ($return_arr as $key=>$val) {
            if ($sku1 == $val['spec_value_id'] && $sku2 == $val['spec_value_id1']) {
                $arr[]['product_id'] = $val['product_id'];
            }
            if ($sku1 == $val['spec_value_id1'] && $sku2 == $val['spec_value_id']) {
                $arr[]['product_id'] = $val['product_id'];
            }
        }
        return $arr;
    }
}
